package Bio::Rambam::Proteomics::Search;
use Moose;
use namespace::autoclean;
use Moose::Util::TypeConstraints;
use Bio::Rambam::Proteomics qw($TEMPLATES);
use Bio::Rambam::Proteomics qw(%ENZYMES);
use Bio::Rambam::Proteomics::Formats::FastaDatabase;
use Bio::Rambam::Proteomics::Searches::OMSSA_search;
use Bio::Rambam::Proteomics::Searches::MYRIMATCH_search;
use Bio::Rambam::Proteomics::Searches::COMET_search;
use Bio::Rambam::Proteomics::Searches::XTANDEM_search;
use File::Temp qw/ tempfile tempdir /;
use Data::Dumper;

subtype 'fragmentations'
               => as enum([qw(CID ETD ECD)]),
               => message {"\n\nOnly three kinds of fragmentation are allowed\n- CID\n- ETD\n- ECD\n\nPlease, choose one of them.\n\n"};

subtype 'enzymes'
               => as enum([qw(Arg-C Trypsin CNBr Chymotrypsin Trypsin/P)]),
               => message {"\n\nOnly five enzymes are allowed\n- Trypsin\n- Trypsin/P\n- Arg-C\n- CNBr\n- Chymotrypsin\n\nPlease, choose one of them.\n\n"};
               
subtype 'mass_units'
               => as enum([qw(Da ppm)]),
               => message {"\n\nPlease, choose among Da and ppm.\n\n"};
               
has 'engines'			=>	(is=>'rw',	isa=>'Bio::Rambam::Proteomics::Engines',default=>sub{{}},);
has 'input_file'		=>	(is=>'rw',	isa=>'Str', required=>1,							);           
has 'folder'			=>	(is=>'rw',	isa=>'Str', required=>1,							);
has 'database'			=>	(is=>'rw',	isa=>'Str',	required=>1,	trigger =>\&_checkDB	);
has 'fragmentation'		=>	(is=>'rw',	isa=>'fragmentations', 		default	=>	'CID'		);
has 'threads'			=>	(is=>'rw',	isa=>'Int', 				default	=>	0			);
has 'enzyme'			=>	(is=>'rw',	isa=>'enzymes',				default	=>	'Trypsin'	);
has 'miss_cleavages'	=>	(is=>'rw',	isa=>'Int',					default	=>	1			);
has 'fixed_mods'		=>	(is=>'rw',	isa=>'Str',					default	=>	''			);
has 'variable_mods'		=>	(is=>'rw',	isa=>'Str',					default	=>	''			);
has 'peptide_tol'		=>	(is=>'rw',	isa=>'Num',					default	=>	2			);
has 'fragment_tol'		=>	(is=>'rw',	isa=>'Num',					default	=>	0.6			);
has 'pep_tol_units'		=>	(is=>'rw',	isa=>'mass_units',			default	=>	'Da'		);
has 'top_peptide_charge'=>	(is=>'rw',	isa=>'Int',					default	=>	3			);
has 'outputs'			=> (is=>'rw', 	isa=>'HashRef[Str]'		,default=>sub{{}});

###############has 'input_file'		=>	(is=>'rw',	isa=>'Str');

sub _checkDB{
	my $self=shift;
	#my $db=Bio::Rambam::Proteomics::Formats::FastaDatabase->new(file=>$self->database);
}

sub exec{
	my ($self)=shift;
	my $search_folder=$self->folder;
	mkdir $search_folder unless -d$search_folder;
	
	if($self->engines->has_omssa){
		my $omssa_search=Bio::Rambam::Proteomics::Searches::OMSSA_search->new(search=>$self);
		${$self->outputs}{omssa}=$omssa_search->output_file;
	} 
	if($self->engines->has_myrimatch){
		my $myrimatch_search=Bio::Rambam::Proteomics::Searches::MYRIMATCH_search->new(search=>$self);	
		${$self->outputs}{myrimatch}=$myrimatch_search->output_file;
	} 
	if($self->engines->has_comet){
		my $comet_search=Bio::Rambam::Proteomics::Searches::COMET_search->new(search=>$self);
		${$self->outputs}{comet}=$comet_search->output_file;	
	} 
	if($self->engines->has_xtandem){
		my $xtandem_search=Bio::Rambam::Proteomics::Searches::XTANDEM_search->new(search=>$self);
		${$self->outputs}{xtandem}=$xtandem_search->output_file;	
	} 
	return (omssa=>${$self->outputs}{omssa},myrimatch=>${$self->outputs}{myrimatch},comet=>${$self->outputs}{comet},xtandem=>${$self->outputs}{xtandem});
}


sub template {
    my ($filename, $fillings) = @_;
    my $text;    
    local $/;
    open my $fh, "<", $filename or return;
    $text = <$fh>;
    close($fh);                  
    $text =~ s{ %% ( .*? ) %% }
              { exists( $fillings->{$1} ) ? $fillings->{$1} : ""}gex;
    return $text;
}




__PACKAGE__->meta->make_immutable;